﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace COFE.Web.NetClient
{
    public class TestRequestEventArgs : EventArgs 
    {
        public string Uri { get; set; }
        public string Method { get; set; }
        public Stream Response { get; set; }
    }

    public class TestNetClient : INetClient
    {
        #region TestWebResponse
        public class TestWebResponse : WebResponse
        {
            internal TestWebResponse(Stream responseStream = null)
            {
                _responseStream = responseStream;
            }

            public override Stream GetResponseStream()
            {                
                return _responseStream != null ? _responseStream : new MemoryStream();
            }
            
            Stream _responseStream;
        }
        #endregion

        #region Constructor



        #endregion

        #region Methods

        public WebResponse SendRequest(string uri, string method, Stream sendStream = null)
        {
            Tuple<string, string> request = new Tuple<string, string>(uri, method);
            if (_responseDic.ContainsKey(request))
                return new TestWebResponse(_responseDic[request]);

            var args = new TestRequestEventArgs()
            {
                Uri = uri,
                Method = method
            };
            OnGetResource(this, args);
            if (args.Response != null)
                return new TestWebResponse(args.Response);

            throw new NotImplementedException(uri + " is not exists.");
        }

        #endregion

        #region Data

        Dictionary<Tuple<string, string>, Stream> _responseDic = new Dictionary<Tuple<string, string>, Stream>();

        #endregion

        #region Public Properties

        public Dictionary<Tuple<string, string>, Stream> ResponseDictionary { get { return _responseDic; } }
        public EventHandler<TestRequestEventArgs> OnGetResource = (o, e) => { };

        #endregion
        
    }
}
